package com.davidk.survey.client.menu;

import com.davidk.survey.client.eventbus.MasterEventBus;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;

@Presenter(view = FunctionMenuView.class)
public class FunctionMenuPresenter extends BasePresenter<FunctionMenuViewInterface, MasterEventBus> {

	@Override
	public void bind() {
		view.getGrid().addSelectionChangedHandler(new SelectionChangedHandler() {
			public void onSelectionChanged(SelectionEvent event) {
				if (event.getState()) {
					// fire event for selected function
					ListGridRecord rec = event.getSelectedRecord();
					String fnFype = rec.getAttribute("Type");
					String fnData = rec.getAttribute("Data");
					eventBus.showFunction(fnFype, fnData);
				} else {
					// fire event for unselected function
				}
			}
		});
	}

	public void onStart() {
	}

	public void onLoginSuccess(String userId) {
		eventBus.changeMenu(view.getView());
		view.fetchData(userId);
	}

	public void onShowFunction(String fnType, String fnData) {
		System.out.println("fnType:" + fnType + ", fnData:" + fnData);
		if (fnType.equals("url"))
			eventBus.changeBody(view.getHtmlView(fnData));
	}
}
